package com.kuang.service.bill;

import com.kuang.dao.BaseDao;
import com.kuang.dao.bill.BillDao;
import com.kuang.dao.bill.BillDaoImpl;
import com.kuang.pojo.Bill;
import com.kuang.pojo.Role;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BillServiceImpl implements BillService{
    //业务层都会调用Dao层，所有我们要引入Dao层
    private BillDao billDao;
    public BillServiceImpl(){
        billDao = new BillDaoImpl();
    }

    //增加订单表
    public boolean add(Bill bill) {
        boolean flag = false;
        Connection connection = null;

        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);
            int updateRows = billDao.add(connection,bill);
            connection.commit();
            if (updateRows > 0){
                flag = true;
                System.out.println("add success!");
            }else {
                System.out.println("add failed!");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                System.out.println("rollback================");
                connection.rollback();//失败就回滚
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }

    //通过条件过去订单列表-模糊查询-billList
    public List<Bill> getBillList(String queryProductName,int queryProviderId,int queryIsPayment) {
        List<Bill> billList = new ArrayList<Bill>();
        Connection connection = null;

        try {
            connection = BaseDao.getConnection();
            billList = billDao.getBillList(connection,queryProductName,queryProviderId,queryIsPayment);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return billList;
    }

    @Test
    public void test(){
        BillServiceImpl billService = new BillServiceImpl();
        List<Bill> billList = billService.getBillList("",2,2);
        System.out.println(billList);
        for (Bill billLists : billList){
            System.out.println(billLists.getProductName());
        }
    }

    //通过billId删除Bill
    public boolean deleteBillById(String delId) {
        boolean flag = false;
        int delNum = 0;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            delNum = billDao.deleteBillById(connection,delId);
            if (delNum > 0){
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }

    //通过billId获取Bill
    public Bill getBillById(String id) {
        Bill bill = new Bill();
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            bill = billDao.getBillById(connection,id);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return bill;
    }

    //修改订单信息
    public boolean modify(Bill bill) {
        boolean flag = false;
        Connection connection = null;
        int modifyNum = 0;
        try {
            connection = BaseDao.getConnection();
            modifyNum = billDao.modify(connection,bill);
            if (modifyNum > 0){
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }
}
